//
//  CZQMySetViewController.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/12.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQMySetViewController.h"
#import "ChangePassViewController.h"
#import "ChangePhoneViewController.h"
#import "PushSetViewController.h"
#import "WebAlertView.h"
#import "ShareAlertView.h"
#import "FeedBackViewController.h"
@interface CZQMySetViewController () <UITableViewDelegate,UITableViewDataSource>

/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

@end

@implementation CZQMySetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"设置";
    
    [self requestData];
    
}


/**
 数据处理
 */
-(void)requestData
{
    
    self.dataArr = [NSMutableArray arrayWithArray:@[@[@"修改密码",@"修改手机号"],@[@"消息设置推送"],@[@"关于我们",@"分享给朋友",@"网页端地址"]]];
    
    [self.tableView reloadData];
    
    
}

#pragma mark -- 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        
        tableView.estimatedRowHeight = 50;
        
        [self.view addSubview:tableView];
        
        
        
        UIButton *footView = [[UIButton alloc] init];
        footView.backgroundColor = CSWhiteColor;
        footView.frame = CGRectMake(0, 0, self.view.width, 50);
        [footView setTitle:@"退出登录"];
        footView.titleLabel.font = MidBigFont;
        [footView addTarget:self action:@selector(loginOut)];
        [footView setTitleColor:CSColorFromRGB(0xee3838)];
        tableView.tableFooterView = footView;
        
        //        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        //                [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQMyTirTableViewCell" bundle:nil]  forCellReuseIdentifier:@"CZQMyTirTableViewCell"];
        
        //        [tableView registerNib:[UINib nibWithNibName:@"YDTrainingTableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
        
        [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSArray *dataArray = [self.dataArr objectAtIndex:section];
    
    return dataArray.count;
    
    
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *dataArray = [self.dataArr objectAtIndex:indexPath.section];
    
    CZQMyTirTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"CZQMyTirTableViewCell"];
    
    cell.titleLable.text = dataArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 10;
    }
    
    return 0.00001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *titleArray = [self.dataArr objectAtIndex:indexPath.section];
    
    NSString *text = [titleArray objectAtIndex:indexPath.row];
    
    if ([text isEqualToString:@"修改手机号"]) {
        
        ChangePhoneViewController *changePhoneVC = [[ChangePhoneViewController alloc] init];
        
        [self.navigationController pushViewController:changePhoneVC animated:YES];
    }
    else if ([text isEqualToString:@"修改密码"]){
        
        ChangePassViewController *changePassVC = [[ChangePassViewController alloc] init];
        
        [self.navigationController pushViewController:changePassVC animated:YES];
    }
    else if ([text isEqualToString:@"消息设置推送"]){
        
        PushSetViewController *pushSetVC = [[PushSetViewController alloc] init];
        
        [self.navigationController pushViewController:pushSetVC animated:YES];
    }
    else if ([text isEqualToString:@"网页端地址"]){
        
        WebAlertView *weAlertView = [[WebAlertView alloc] init];

        [weAlertView showWithAnimation];
    }
    else if ([text isEqualToString:@"分享给朋友"]){
        
        ShareAlertView *shareAlertView = [[ShareAlertView alloc] init];
        shareAlertView.weChatBtn.tag = 0;
        shareAlertView.friendBtn.tag = 1;
        shareAlertView.linkBtn.tag = 2;
        [shareAlertView.weChatBtn addTarget:self action:@selector(shareBtnClick:)];
        [shareAlertView.friendBtn addTarget:self action:@selector(shareBtnClick:)];
        [shareAlertView.linkBtn addTarget:self action:@selector(shareBtnClick:)];
        [shareAlertView showWithAnimation];
    }
    else if ([text isEqualToString:@"意见反馈"]){
        
        FeedBackViewController *feedBackVC = [[FeedBackViewController alloc] init];

        [self.navigationController pushViewController:feedBackVC animated:YES];
    }
    else if([text isEqualToString:@"关于我们"]){
        
        [MBProgressHUD showText:@"暂未开发" toview:self.view];
        
        NSArray *titleArray = [self.dataArr objectAtIndex:indexPath.section];
        
        NSString *text = [titleArray objectAtIndex:indexPath.row];
        
        
    }
}

-(void)loginOut
{
    [JHSysAlertUtil presentAlertViewWithTitle:@"温馨提示" message:@"确定退出?" cancelTitle:@"取消" defaultTitle:@"确定" distinct:NO cancel:^{
        
    } confirm:^{
        
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        
        [userDefault removeObjectForKey:@"isLogin"];
        
        [userDefault removeObjectForKey:@"Token"];
        
        [userDefault removeObjectForKey:@"TLSToken"];
        
        [userDefault removeObjectForKey:@"UserID"];
        
        [userDefault removeObjectForKey:@"isCompleteInfo"];
        
        [userDefault synchronize];
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        
        UserNavigationController *naVC = [[UserNavigationController alloc] initWithRootViewController:loginVC];
        
        naVC.isLoginNav = YES;
        //
//        IMAAppDelegate *appDelegate = [IMAAppDelegate sharedAppDelegate];
        
        [UIApplication sharedApplication].keyWindow.rootViewController = naVC;
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
